package nl.iprofs.modules.contextlogging;
/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.net.URL;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.BasicConfigurator;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

public class LogbackClasspathConfigurator {
    private static final Logger LOG = LoggerFactory.getLogger(LogbackClasspathConfigurator.class);

    private static boolean initialized;

    public static synchronized void init(ServletConfig servletConfig) throws ServletException {
        init(servletConfig.getServletContext());
    }

    public static synchronized void init(ServletContext servletContext) throws ServletException {
        if (!initialized) {
            URL logbackConfigLocation = getLogbackConfigLocation(servletContext);

            LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

            if (logbackConfigLocation == null) {
                BasicConfigurator.configureDefaultContext();
                LOG.info("No context-specific configuration file found, will use Logback's default configuration");
            } else {
                JoranConfigurator configurator = new JoranConfigurator();
                configurator.setContext(lc);
                // the context was probably already configured by default configuration rules
                lc.reset();
                try {
                    configurator.doConfigure(logbackConfigLocation);
                } catch (JoranException je) {
                    // StatusPrinter will handle this
                }
                
            }
            StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
            initialized = true;
        }
    }

    /**
     * This is where the actual config file is determined
     */
    private static URL getLogbackConfigLocation(ServletContext servletContext) throws ServletException {
        String contextPath = servletContext.getContextPath();
        if (LOG.isDebugEnabled()) {
            LOG.debug("contextPath: " + contextPath);
        }

        if ("".equals(contextPath)) {
            contextPath = "/ROOT";
        }
        String logbackFile = contextPath + "-logback.xml";
        if (LOG.isDebugEnabled()) {
            LOG.debug("Logback filename: " + logbackFile);
        }
        return LogbackClasspathConfigurator.class.getResource(logbackFile);
    }

}
